/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Maestros.DAO;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pe.edu.pucp.Modelo.Cliente;
import pe.edu.pucp.Modelo.Envio;
import pe.edu.pucp.Util.HibernateUtil;

/**
 *
 * @author oscar
 */
public class EnvioDAO {
    public void guardarEnvio(Envio c) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        session.save(c);
        tx.commit();
        session.close();
    }
    
    public Cliente daCliente (int id){
        Cliente a = new Cliente();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        
        try {
            tx = session.beginTransaction();

            // Prepara cadena query
            String q = "select cliente from Cliente cliente where  IdCliente =:idClienteParam";

            // Crear query
            Query query = session.createQuery(q);
                  query.setParameter("idClienteParam", id);
            
            // Ejecutar query
            List lista = query.list();
            for ( Iterator iter = lista.iterator(); iter.hasNext(); ) {    
                a = (Cliente) iter.next();
                Hibernate.initialize(a.getApmaterno());
                Hibernate.initialize(a.getAppaterno()); 
//                Hibernate.initialize(a.getCiudad());
//                Hibernate.initialize(a.getCiudad().getPais());
//                Hibernate.initialize(a.getCiudad().getPais().getContinente());
//                Hibernate.initialize(a.getEstadoAeropuerto());              
            }
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
                throw e;
           }
        } finally {
            session.close();

            return a;
        }
        
        
        
    }
}
